package com.bio.test2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
 * 客户端
 * @author gxkj-042
 *
 */
public class Client {
	final static String ADDRESS = "127.0.0.1";
	final static int PORT = 8765;
	
	public static void main(String[] args) {
		Socket socket = null;
		BufferedReader reader = null;
		PrintWriter writer = null;
		for(int i = 0; i < 1000; i++){
			try {
				System.out.println("客户端启动...");
				socket = new Socket(ADDRESS, PORT);
				reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				writer = new PrintWriter(socket.getOutputStream(),true);
				
				//向服务端发送消息
				writer.println("我要发送消息..."+i);
				String body = reader.readLine();
				System.out.println("客户端接收信息："+body);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(socket != null){
					try {
						socket.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if(reader != null){
					try {
						reader.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if(writer != null){
					try {
						writer.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
